C
      FUNCTION XSQ(N,MAX1)
C
C     THIS FUNCTION COMPUTES THE CHI-SQUARE STATISTIC
C
C     ******************************************************************
C
      DIMENSION FO(128),FE(128),Z(5)
      COMMON/PROB/PRBXAY(128,128),PRBX(128),PRBY(128),PRBXGY(128,128),
     .   XMUT(128,128),XINFYI(128)
      DATA Z/1.28,1.645,2.33,2.58,2.88/
C
      M=0
      SUMEXP=0.0
      SUMOBS=0.0
      FN=FLOAT(N)
C
C     COMPUTE THE OBSERVED AND EXPECTED VALUES
      DO 20 I=1,MAX1
      SUMOBS=SUMOBS+FN*PRBY(I)
      SUMEXP=SUMEXP+FN*PRBX(I)
      IF(SUMEXP.GT.5.0) GO TO 10
      GO TO 20
   10 M=M+1
      L=I
C
C     STORE THE OBSERVED AND EXPECTED VALUES
      FO(M)=SUMOBS
      FE(M)=SUMEXP
      SUMEXP=0.0
      SUMOBS=0.0
   20 CONTINUE
      IF(L.EQ.MAX1) GO TO 40
C
C     ADD LAST VALUES LESS THAN FIVE TO PREVIOUS CLASS
      IF(M.EQ.0) GO TO 70
      FO(M)=FO(M)+SUMOBS
      FE(M)=FE(M)+SUMEXP
C
C     COMPUTE CHI-SQUARE STATISTIC
   40 XSQ=0.0
      WRITE(6,600)(FE(J),J=1,M)
      DO 50 I=1,M
      DIFSQ=(FO(I)-FE(I))**2
      DIFOEX=DIFSQ/FE(I)
      XSQ=XSQ+DIFOEX
   50 CONTINUE
      WRITE(6,100) XSQ
      NDF=M-1
      WRITE(6,200) NDF
      FNDF=FLOAT(NDF)
      XCALC=(XSQ/FNDF)**(1.0/3.0)
      WRITE(6,300) XCALC
      DO 60 I=1,5
      TWO9N=2.0/(9.0*FNDF)
      XTAB=1.0-TWO9N+Z(I)*SQRT(TWO9N)
      WRITE(6,400) XTAB,Z(I)
   60 CONTINUE
      GO TO 80
   70 WRITE(6,500)
   80 RETURN
C
  100 FORMAT(//,1X,'CHI-SQUARE=',F12.5)
  200 FORMAT(//,1X,'DEGREES OF FREEDOM=',I12)
  300 FORMAT(//,1X,'X**2/N**(1/3)=',F12.5,/)
  400 FORMAT(1X,'1-2/9N+Z(2/9N)**(1/2)=',F12.5,2X,'AT Z=',F12.5)
  500 FORMAT(1X,'THE SUM OF ALL CLASSES IS LESS THAN FIVE')
  600 FORMAT(1X,10F10.1)
      END
